package org.my.stock.h2;


import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import org.my.stock.h2.util.MyUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MaiDian1 {
    public static List getList(String line) {
        Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
        Matcher matcher = pattern.matcher(line);

        if (matcher.find()) {
            String result = matcher.group(1); // 获取第一个捕获组内容

            LinkedTreeMap m = MyUtil.getGson().fromJson(result, new TypeToken<LinkedTreeMap>() {}.getType());
            LinkedTreeMap data = (LinkedTreeMap) m.get("data");
            List klines = (List) data.get("klines");

            return klines;
        }
        return null;
    }

    public static List<Object> getData(String content) {
        List<String> lst = MaiDian.getList(content);
        if (lst != null) {
            List<Object> rs = new ArrayList<>();
            for (int j = lst.size() - 1; j > -1; j--) {
                String line = lst.get(j);
                List<Object> ls = new ArrayList<>();
                String[] ss = line.split(",");
                for (int i = 0; i < ss.length; i++) {
                    if (i == 0) {
                        ls.add(ss[i]);
                    } else {
                        ls.add(Float.valueOf(ss[i]));
                    }
                }
                rs.add(ls);
            }
            return rs;
        }
        return null;
    }

    public static List<Object> getData(List<String> lst) {
        //List<String> lst = MaiDian.getList(content);
        if (lst != null) {
            List<Object> rs = new ArrayList<>();
            for (int j = lst.size() - 1; j > -1; j--) {
                String line = lst.get(j);
                List<Object> ls = new ArrayList<>();
                String[] ss = line.split(",");
                for (int i = 0; i < ss.length; i++) {
                    if (i == 0) {
                        ls.add(ss[i]);
                    } else {
                        ls.add(Float.valueOf(ss[i]));
                    }
                }
                rs.add(ls);
            }
            return rs;
        }
        return null;
    }

    public static List<Integer> get_low(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {

//            List<Object> row_0 = (List<Object>)lst.get(i);
//            if (row_0.get(0).toString().equals("2025-06-03")) {
//                System.out.println(row_0);
//            }

            if ((i - 5) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);
                float v1 = get_average_1(lst, i);
                float v2 = get_average_2(lst, i);

                float v13 = get_average_13(lst, i);
                float v23 = get_average_23(lst, i);
                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v1 && (price < v2 || price_2 < v2) && price < v13 && (price < v23 || price_2 < v23)) {
                    rs.add(i);
                }
            } else if ((i - 2) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);

                if (price < price_1 && price < price_11 && price < price_2 && price < price_21) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static List<Integer> get_low_ex(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {

//            List<Object> row_0 = (List<Object>)lst.get(i);
//            if (row_0.get(0).toString().equals("2025-06-23")) {
//                System.out.println(row_0);
//            }

            if ((i - 4) >= 0 && (i + 4) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_12 = (List<Object>)lst.get(i - 3);
                List<Object> row_13 = (List<Object>)lst.get(i - 4);
                //List<Object> row_14 = (List<Object>)lst.get(i - 5);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                List<Object> row_22 = (List<Object>)lst.get(i + 3);
                List<Object> row_23 = (List<Object>)lst.get(i + 4);
                //List<Object> row_24 = (List<Object>)lst.get(i + 5);

                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_11 = (float)row_11.get(11);
                float price_12 = (float)row_12.get(11);
                float price_13 = (float)row_13.get(11);
                //float price_14 = (float)row_14.get(11);

                float price_2 = (float)row_2.get(11);
                float price_21 = (float)row_21.get(11);
                float price_22 = (float)row_22.get(11);
                float price_23 = (float)row_23.get(11);
                //float price_24 = (float)row_24.get(11);

//                if (price < price_1 && price < price_11 && price < price_12 && price < price_13 && price < price_14 && price < price_2 && price < price_21 && price < price_22 && price < price_23 && price < price_24) {
//                    rs.add(i);
//                }
                if ((float)row.get(12) != 0F && price <= price_1 && price <= price_11 && price <= price_12 && price <= price_13 && price <= price_2 && price <= price_21 && price <= price_22 && price <= price_23) {
                    rs.add(i);
                }

//                float v1 = get_average_1(lst, i);
//                float v2 = get_average_2(lst, i);
//
//                float v13 = get_average_13(lst, i);
//                float v23 = get_average_23(lst, i);
//                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v1 && (price < v2 || price_2 < v2) && price < v13 && (price < v23 || price_2 < v23)) {
//                    rs.add(i);
//                }
            } else if ((i - 2) >= 0 && (i + 4) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);

                if ((float)row.get(12) != 0F && price <= price_1 && price <= price_11 && price <= price_2 && price <= price_21) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static List<Integer> get_height_ex(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {

//            List<Object> row_0 = (List<Object>)lst.get(i);
//            if (row_0.get(0).toString().equals("2025-06-03")) {
//                System.out.println(row_0);
//            }

            if ((i - 4) >= 0 && (i + 4) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_12 = (List<Object>)lst.get(i - 3);
                List<Object> row_13 = (List<Object>)lst.get(i - 4);
                //List<Object> row_14 = (List<Object>)lst.get(i - 5);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                List<Object> row_22 = (List<Object>)lst.get(i + 3);
                List<Object> row_23 = (List<Object>)lst.get(i + 4);
                //List<Object> row_24 = (List<Object>)lst.get(i + 5);

                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_11 = (float)row_11.get(11);
                float price_12 = (float)row_12.get(11);
                float price_13 = (float)row_13.get(11);
                //float price_14 = (float)row_14.get(11);

                float price_2 = (float)row_2.get(11);
                float price_21 = (float)row_21.get(11);
                float price_22 = (float)row_22.get(11);
                float price_23 = (float)row_23.get(11);
                //float price_24 = (float)row_24.get(11);

                // (price < price_1 && price < price_11 && price < price_12 && price < price_13 && price < price_14 && price > price_2 && price > price_21 && price > price_22 && price > price_23 && price > price_24) {
                if ((float)row.get(12) != 0F && price >= price_1 && price >= price_11 && price >= price_12 && price >= price_13 && price >= price_2 && price >= price_21 && price >= price_22 && price >= price_23) {
                    rs.add(i);
                }

//                float v1 = get_average_1(lst, i);
//                float v2 = get_average_2(lst, i);
//
//                float v13 = get_average_13(lst, i);
//                float v23 = get_average_23(lst, i);
//                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v1 && (price < v2 || price_2 < v2) && price < v13 && (price < v23 || price_2 < v23)) {
//                    rs.add(i);
//                }
            } else if ((i - 2) >= 0 && (i + 4) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);

                // (price < price_1 && price < price_11 && price < price_2 && price < price_21) {
                if ((float)row.get(12) != 0F && price >= price_1 && price >= price_11 && price >= price_2 && price >= price_21) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static List<Integer> get_height(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {

//            List<Object> row_0 = (List<Object>)lst.get(i);
//            if (row_0.get(0).toString().equals("2025-06-03")) {
//                System.out.println(row_0);
//            }

            if ((i - 5) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);
                float v1 = get_average_1(lst, i);
                float v2 = get_average_2(lst, i);

                float v13 = get_average_13(lst, i);
                float v23 = get_average_23(lst, i);
                // (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v1 && (price < v2 || price_2 < v2) && price < v13 && (price < v23 || price_2 < v23)) {
                if (((price > price_1 && price < price_2) || (price > price_11 && price < price_21)) && price > v1 && (price > v2 || price_2 > v2) && price > v13 && (price > v23 || price_2 > v23)) {
                    rs.add(i);
                }
            } else if ((i - 2) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);

                // (price < price_1 && price < price_11 && price < price_2 && price < price_21) {
                if (price > price_1 && price > price_11 && price > price_2 && price > price_21) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static List<Integer> get_low_1(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {
            if ((i - 5) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);
                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(11);
                float price_1 = (float)row_1.get(11);
                float price_2 = (float)row_2.get(11);
                float price_11 = (float)row_11.get(11);
                float price_21 = (float)row_21.get(11);
                //float v1 = get_average_1(lst, i);
                //float v2 = get_average_2(lst, i);

                float v13 = get_average_13(lst, i);
                float v23 = get_average_23(lst, i);
                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v13 && (price < v23 || price_1 < v23)) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static Float get_average_1(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index - 1);
        List<Object> row_2 = (List<Object>)lst.get(index - 2);
        List<Object> row_3 = (List<Object>)lst.get(index - 3);
        List<Object> row_4 = (List<Object>)lst.get(index - 4);
        List<Object> row_5 = (List<Object>)lst.get(index - 5);

        float price_1 = (float)row_1.get(11);
        float price_2 = (float)row_2.get(11);
        float price_3 = (float)row_3.get(11);
        float price_4 = (float)row_4.get(11);
        float price_5 = (float)row_5.get(11);

        return (Float) ((price_1 + price_2 + price_3 + price_4 + price_5) / 5);
    }

    public static Float get_average_2(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index + 1);
        List<Object> row_2 = (List<Object>)lst.get(index + 2);
        List<Object> row_3 = (List<Object>)lst.get(index + 3);
        List<Object> row_4 = (List<Object>)lst.get(index + 4);
        List<Object> row_5 = (List<Object>)lst.get(index + 5);

        float price_1 = (float)row_1.get(11);
        float price_2 = (float)row_2.get(11);
        float price_3 = (float)row_3.get(11);
        float price_4 = (float)row_4.get(11);
        float price_5 = (float)row_5.get(11);

        return (Float) ((price_1 + price_2 + price_3 + price_4 + price_5) / 5);
    }

    public static Float get_average_13(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index - 1);
        List<Object> row_2 = (List<Object>)lst.get(index - 2);
        List<Object> row_3 = (List<Object>)lst.get(index - 3);

        float price_1 = (float)row_1.get(11);
        float price_2 = (float)row_2.get(11);
        float price_3 = (float)row_3.get(11);

        return (Float) ((price_1 + price_2 + price_3) / 3);
    }

    public static Float get_average_23(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index + 1);
        List<Object> row_2 = (List<Object>)lst.get(index + 2);
        List<Object> row_3 = (List<Object>)lst.get(index + 3);

        float price_1 = (float)row_1.get(11);
        float price_2 = (float)row_2.get(11);
        float price_3 = (float)row_3.get(11);

        return (Float) ((price_1 + price_2 + price_3) / 3);
    }

    public static List<Float> getK(List<Object> lst, List<Integer> rs) {
        List<Float> lsrs = new ArrayList<>();
        for (int i = 0; i < rs.size(); i += 2) {
            if (i + 1 < rs.size()) {
                int first = rs.get(i);
                int end = rs.get(i + 1);

                List<Object> row = (List<Object>) lst.get(first);
                List<Object> row_1 = (List<Object>) lst.get(end);
                Float h = (Float) row.get(11) - (Float) row_1.get(11);
                float myrs = h / (end - first);
                lsrs.add(myrs);
            }
        }
        return lsrs;
    }

    public static Float get_price_x(List<Object> lst, List<Integer> rs, List<Float> lsrs) {
        Float k = lsrs.get(0);
        Integer index = rs.get(0);
        List<Object> end_row = (List<Object>)lst.get(index);
        Float price = (Float)end_row.get(11);
        return price + k * index;
    }
}
